package com.jinxiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinxiangyang.domain.Chapter;
import com.jinxiangyang.domain.Video;
import com.jinxiangyang.domain.vo.ChapterVo;
import com.jinxiangyang.domain.vo.VideoVo;
import com.jinxiangyang.mapper.ChapterMapper;
import com.jinxiangyang.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinxiangyang.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {



    @Autowired
    private VideoService videoService;

    @Override
    public List<Chapter> getChapterByCid(Integer cid){
        return  null;
    }

    @Override
    public List<ChapterVo> getTreeList(Long courseId) {

        List<ChapterVo> chapterVoList = new ArrayList<>();
        LambdaQueryWrapper<Chapter> chapterWapper = new LambdaQueryWrapper<>();


        chapterWapper.eq(Chapter::getCourseId,courseId);
        List<Chapter> chapterList= baseMapper.selectList(chapterWapper);
        LambdaQueryWrapper<Video> videoQueryWrapper = new LambdaQueryWrapper<>();
        videoQueryWrapper.eq(Video::getCourseId,courseId);
        List<Video> videoList = videoService.list(videoQueryWrapper);


        chapterList.forEach(item -> {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(item,chapterVo);
          List<VideoVo> videoVoList = new ArrayList<>();
            for (Video video : videoList) {
                VideoVo videoVo = new VideoVo();
                if (video.getChapterId().equals(chapterVo.getId())) {
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            chapterVoList.add(chapterVo);
        });
        return chapterVoList;
    }

    @Override
    public void removeChapterCourseId(Long id) {
        LambdaQueryWrapper<Chapter> wrap = new LambdaQueryWrapper();

        wrap.eq(Chapter::getCourseId,id);
        baseMapper.delete(wrap);
    }


}
